<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>arrayuse</title>
</head>
<body>
    <!-- 数组的使用 -->
    <script>
        // 数组的成员函数

        // slice(iStart[,iEnd]); 从start开始，到end结束。
        // 节选数组种的一段，原数组不受影响；
        var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8];
        document.write('arr = [0, 1, 2, 3, 4, 5, 6, 7, 8]');
        console.log('arr.slice(6) = ' + arr.slice(6)); // 6,7,8
        console.log('arr.slice(6).join(",") = ' + arr.slice(6).join('-')); // 6-7-8
        console.log('arr.slice(-3) = ' + arr.slice(-3)); // 6,7,8
        console.log('arr.slice(2, 5) = ' + arr.slice(2, 5)); // 2,3,4
        console.log('arr.slice(-7, 5) = ' + arr.slice(-7, 5)); // 2,3,4

        // splice(iIndex[, iHowmany],[, item1],[, item2],[, item3]....)
        // 从 iIndex开始，删除元素，删除几个由iHowmany决定；原数组受到影响
        // howmany	必需。要删除的项目数量。如果设置为 0，则不会删除项目
        // 可以切出元素并更换元素
        console.log('==========splice==========');
        arr = [0, 1, 2, 3, 4, 5, 6, 7, 8];
        console.log('arr = ' + arr);
        console.log('arr.length  = ' + arr.length);
        var arrP = arr.splice(6);
        console.log('arr.splice(6) = ' + arrP);
        console.log('arr = ' + arr);
        console.log('arr.length = ' + arr.length);
        console.log('====================');
        arr = [0, 1, 2, 3, 4, 5, 6, 7, 8];
        console.log('arr = ' + arr);
        arrP = arr.splice(2, 3);
        console.log('arr.splice(2, 3) = ' + arrP);
        console.log('arr = ' + arr);
        console.log('===================');
        arr = [0,1,2,3,4,5,6,7,8];
        console.log('arr = ' + arr);
        arr1 = arr.splice(2,3,-1,-2,-3,-4);
        console.log('arr1 = ' + arr1);
        console.log('arr.splice(2,3,-1,-2,-3,-4) = ' + arr);
        console.log('arr1 = ' + arr1);

        // delete // 删除元素会影响原数组
        console.log('============delete==========')
        arr = [0, 2, 3, 4, 5, 6, 7, 8];
        console.log('arr = ' + arr);
        delete arr[4]; // 删除长度
        console.log('arr = ' + arr);
        console.log('arr.length = ' + arr.length);
    </script>
</body>
</html>